import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_shark_im/entity/message/authentication_entity.dart';
import 'package:flutter_shark_im/entity/message/authentication_response_entity.dart';
import '../main.dart';

class LoginPageState extends State<ApplicationPage> {

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  LoginPageState();

  @override
  void initState() {
    super.initState();
    widget.eventBus.on<AuthenticationResponseEntity>().listen((data) {
      SharkIm.id = data.name;
      if (data.result == 1) {
        Navigator.of(context).pushNamed("/home");
      }
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
            child: new Scaffold(
              body: Center(
                child: ListView(
                  padding: EdgeInsets.symmetric(horizontal: 22.0),
                  children: <Widget>[
                    GestureDetector(
                      child: AnimatedSwitcher(
                        transitionBuilder: (child, anim) {
                          return ScaleTransition(child: child, scale: anim);
                        },
                        duration: Duration(milliseconds: 300),
                        child: Padding(
                          key: ValueKey<int>(SharkIm.avatar),
                          padding: EdgeInsets.all(8.0),
                          child: new Image.asset('asserts/' + SharkIm.avatar.toString() +'.png'),
                        ),
                      ),
                      onTap: () {
                        setState(() {
                          SharkIm.avatar = new Random().nextInt(10);
                        });
                      },
                    ),
                    SizedBox(height: 20.0),
                    Form (
                      key: _formKey,
                      child: TextFormField(
                        decoration: InputDecoration(
                            labelText: '昵称',
                            labelStyle: TextStyle(
                                fontSize: 23.0,
                                height: 0
                            ),
                        ),
                        initialValue: SharkIm.name,
                        validator: (val) => (val == null || val.isEmpty) ? "请输入昵称": null,
                        onSaved: (val) => SharkIm.name = val,
                      ),
                    ),
                    SizedBox(height: 40.0,),
                    FlatButton(
                      onPressed: () {
                        if (_formKey.currentState.validate()) {
                          _formKey.currentState.save();
                          final msg = AuthenticationEntity(name: SharkIm.name, avatar: SharkIm.avatar.toString());
                          widget.eventBus.fire(msg.toJson());
                        }
                      },
                      child: Text(
                        '进入',
                        style: TextStyle(
                          color: Colors.white,
                        ),
                      ),
                      color: Theme.of(context).accentColor,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20))
                      ),
                    )
                  ],
                ),
              ),
            )
        )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}


